package cpa.prowon.gui;

import java.util.Properties;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pho.prowon.category.RootIndexCategory;
import cpa.prowon.Application;
import cpa.prowon.gui.util.ImageUtil;

/**
 * Open the configuration/preferences window to change the preferences. The
 * preferences include currently the reload index on startup, show thumbnail and
 * thumbnail size
 * @author Chris
 */
public class ConfigurationWindow {

	// Properties
	protected MainWindow window;
	protected ResourceBundle rb;
	protected Shell shell;
	protected Button field_refreshindex;
	protected Button field_showpreview;
	protected Text field_thumbnailsize;
	protected Properties appProps;
	
	/**
	 * Property name constant for the x value of the window
	 * @return The property name for the x value of the window
	 * @author Chris
	 */
	protected static final String PROPNAME_X = "prowon.ConfigurationWindow.x";

	/**
	 * Property name constant for the y value of the window
	 * @return The property name for the y value of the window
	 * @author Chris
	 */
	protected static final String PROPNAME_Y = "prowon.ConfigurationWindow.y";

	/**
	 * Constructor for PreferencesWindow
	 * @param MainWindow The main window which is the parent window for the configuration window
	 * @author Chris
	 */
	public ConfigurationWindow(MainWindow mainWindow) {
		
		// Set the window
		window = mainWindow;
		
		// Get the resource bundle
		rb = Application.getInstance().getResourceBundle();
		
		// Get the application properties
		appProps = Application.getInstance().getApplicationProperties();
		
	}
	
	/**
	 * Open the window
	 * @author Chris
	 */
	public void open() {
	
		// Create the and configure the shell
		shell = createShell();
		configureShell();

		// Create the contents
		createContents(shell);
				
		// Open the shell
		shell.pack();
		shell.open ();
	
		// Dispose the shell
		Display display = shell.getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
	
	}

	/**
	 * Handle the situation when the window get closed
	 * In this case the window settings get saved
	 * @author Chris
	 */
	public void cleanup() {
	
		// Save the window information
		Point location = shell.getLocation();
		String x = "" + location.x;
		String y = "" + location.y;
		Properties appProps = Application.getInstance().getApplicationProperties();
		appProps.setProperty(ConfigurationWindow.PROPNAME_X, x);
		appProps.setProperty(ConfigurationWindow.PROPNAME_Y, y);
	
	}
	
	/**
	 * Create the contents for the window
	 * @author Chris
	 */
	protected void createContents(Shell parent) {
		
		// Create the index information group
		Group index_group = new Group(shell, SWT.NONE);
		GridData index_griddata = new GridData(GridData.FILL_HORIZONTAL);
		index_griddata.horizontalSpan = 2;
		index_group.setLayoutData(index_griddata);
		GridLayout index_gridlayout = new GridLayout ();
		index_gridlayout.marginWidth = 10;
		index_gridlayout.numColumns = 2;
		index_group.setLayout(index_gridlayout);
		index_group.setText(rb.getString("property_grouptitle_index"));
		
		// Create the refresh index on startup field
		field_refreshindex = createCheckboxField(index_group, rb.getString("property_title_refreshindex"));
		if (appProps.getProperty(RootIndexCategory.PROPNAME_REFRESHINDEX).equals("true")) {
			field_refreshindex.setSelection(true);
		}
		
		// Create the preview information group
		Group preview_group = new Group(shell, SWT.NONE);
		GridData preview_griddata = new GridData(GridData.FILL_HORIZONTAL);
		preview_griddata.horizontalSpan = 2;
		preview_group.setLayoutData(preview_griddata);
		GridLayout preview_gridlayout = new GridLayout ();
		preview_gridlayout.marginWidth = 10;
		preview_gridlayout.numColumns = 2;
		preview_group.setLayout(preview_gridlayout);
		preview_group.setText(rb.getString("property_grouptitle_preview"));
		
		// Create the show preview field
		field_showpreview = createCheckboxField(preview_group, rb.getString("property_title_showthumbnails"));
		if (appProps.getProperty(MainWindow.PROPNAME_SHOWTHUMBNAIL).equals("true")) {
			field_showpreview.setSelection(true);
		}
		
		// Create the index location field
		createLabel(preview_group, rb.getString("property_title_thumbnailsize"), 1);
		field_thumbnailsize = createSingleTextField(preview_group, appProps.getProperty(MainWindow.PROPNAME_THUMBNAILSIZE));
		
		// Create the OK button
		Button okButton = new Button(shell, SWT.PUSH);
		GridData okButtonGridData = new GridData();
		okButtonGridData.verticalSpan = 8;
		okButtonGridData.horizontalAlignment = GridData.END;
		okButtonGridData.widthHint = 100;
		okButton.setLayoutData(okButtonGridData);
		okButton.setText(Application.getInstance().getResourceBundle().getString("button_ok"));
		okButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				
				// Set the refresh index on startup property
				String newRefreshIndex = field_refreshindex.getSelection() + "";
				appProps.setProperty(RootIndexCategory.PROPNAME_REFRESHINDEX, newRefreshIndex);
				
				// Set the show thumbnails option
				String newShowThumbNails = field_showpreview.getSelection() + "";
				appProps.setProperty(MainWindow.PROPNAME_SHOWTHUMBNAIL, newShowThumbNails);
				
				// Set the thumbnail size
				String newThumbNailSize = field_thumbnailsize.getText();
				appProps.setProperty(MainWindow.PROPNAME_THUMBNAILSIZE, newThumbNailSize);
				
				// Save the properties
				Application.getInstance().saveApplicationProperties();

				// Close the window
				close();
			}

		});
	
		// Create the Cancel button
		Button cancelButton = new Button(shell, SWT.PUSH);
		GridData cancelButtonGridData = new GridData();
		cancelButtonGridData.verticalSpan = 8;
		cancelButtonGridData.horizontalAlignment = GridData.BEGINNING;
		cancelButtonGridData.widthHint = 100;
		cancelButton.setLayoutData(cancelButtonGridData);
		cancelButton.setText(Application.getInstance().getResourceBundle().getString("button_cancel"));
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				close();
			}
		});
	
	}

	/**
	 * Close the window. Before closing the window do some cleanup work and
	 * remember the settings of the window, like position
	 * @author Chris
	 */
	protected void close() {
		cleanup();
		shell.dispose();
	}
	
	/**
	 * Create a single text entry field
	 * @param Composite The parent where the text field shall be put into
	 * @param String The initial value of the text field
	 * @return The text field
	 * @author Chris
	 */
	protected Text createSingleTextField(Composite parent, String value) {
		
		// Create the text field
		final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		
		// Layout the text field
		GridData grid = new GridData();
		grid.grabExcessHorizontalSpace = true;
		grid.horizontalAlignment = GridData.FILL;
		grid.widthHint = 200;
		text.setLayoutData(grid);
		
		// Set the initial text
		text.setText(value);
		
		// Return the text field
		return text;
	}
	
	/**
	 * Create a checkbox entry field
	 * @param Composite The parent where the text field shall be put into
	 * @param String The initial value of the text field
	 * @return The actual checkbox
	 * @author Chris
	 */
	protected Button createCheckboxField(Composite parent, String value) {
		
		// Create the checkbox
		Button checkbox = new Button(parent, SWT.CHECK);

		// Layout the checkbox
		GridData grid = new GridData();
		grid.verticalAlignment = GridData.BEGINNING;
		grid.horizontalSpan = 2;
		checkbox.setLayoutData(grid);

		// Set the check box text
		checkbox.setText(value);
		
		// Return the checkbox		
		return checkbox;

	}
	
	/**
	 * Create a label with the appropriate formatting
	 * @param Composite The parent where the text field shall be put into
	 * @param String The value of the label
	 * @return The label
	 * @author Chris
	 */
	protected Label createLabel(Composite parent, String value, int colSpan) {
		
		// Create the label
		Label label = new Label(parent, 1);
		
		// Layout the label
		GridData grid = new GridData();
		grid.verticalAlignment = GridData.BEGINNING;
		grid.horizontalSpan = colSpan;
		label.setLayoutData(grid);
		
		// Set the label text
		label.setText(value);
		
		// Return the label
		return label;
		
	}

	/**
	 * Configure the shell
	 * @author Chris
	 */
	protected void configureShell() {
		
		// Configure depending on the last remembered information
		Properties appProps = Application.getInstance().getApplicationProperties();
		
		// Set the location
		String x = appProps.getProperty(ConfigurationWindow.PROPNAME_X, "");
		String y = appProps.getProperty(ConfigurationWindow.PROPNAME_Y, "");
		if (x != "" & y != "") {
			int ix = Integer.parseInt(x);
			int iy = Integer.parseInt(y);
			shell.setLocation(ix, iy);
		}
		
	}
	
	/**
	 * Create the shell that holds all the window widgest
	 * @return The actual shell holding all widgets
	 * @author Chris
	 */
	protected Shell createShell() {
	
		// Create the shell
		Shell shell = new Shell(window.getShell(), SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
		GridLayout shellGrid = new GridLayout();
		shellGrid.marginWidth = 10;
		shellGrid.marginHeight = 5;
		shellGrid.makeColumnsEqualWidth = true;
		shellGrid.numColumns = 2;
		shell.setLayout(shellGrid);
		shell.setText (Application.getInstance().getResourceBundle().getString("title_configuration"));
		shell.setImage(ImageUtil.getImage("icons/logo.gif"));
	
		// Return the shell
		return shell;
		
	}

}
